package com.monster.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.monster.dao.PlaylistDao;
import com.monster.dao.PlaylistDaoImpl;
import com.monster.pojo.Playlist;
import com.monster.pojo.Song;
import com.monster.service.UserService;
import com.monster.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

@WebServlet("/textServlet")
public class textServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String newSongId = request.getParameter("songId");
        String newPlaylistId = request.getParameter("playlistId");
        PrintWriter out = response.getWriter();
        Long songId=Long.parseLong(newSongId);
        Long playlistId = Long.parseLong(newPlaylistId);
        Playlist playlist = new Playlist();
        playlist.setPlaylistId(playlistId);
        UserService userService = new UserServiceImpl();
        //查找歌单里面的歌的songId集合
        PlaylistDao playlistDao = new PlaylistDaoImpl();
        ArrayList<Song> song =  playlistDao.selectSongIdByPlaylistId(playlistId);
        String result = "{\"data\":\"no\"}";
        //导入
        //String strJSON = JSON.toJSONString(result);
        for(Song songList:song){
            if(songId==songList.getSongId()){
                result = "{\"data\":\"no\"}";
                break;
            }else{
                result ="{\"data\":\"yes\"}";
            }
        }
        if(result=="{\"data\":\"yes\"}"){
            playlistDao.addPlaylistSong(playlist,songId);
        }
        out.print(result);
        out.flush();
        out.close();
    }
}
